//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.qst.crop.security.util;

import com.qst.crop.security.entity.JwtUser;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class JwtTokenUtil {
    private String secret = "qst123456";
    private static final Long expiration = 3600000L;
    private static final String header = "Authorization";

    private String generateToken(Map<String, Object> claims) {
        Date expirationDate = new Date(System.currentTimeMillis() + expiration);
        return Jwts.builder().setClaims(claims).setExpiration(expirationDate).signWith(SignatureAlgorithm.HS512, this.secret).compact();
    }

    public static String getHeader() {
        return "Authorization";
    }

    private Claims getClaimsFromToken(String token) {
        Claims claims;
        try {
            claims = (Claims)Jwts.parser().setSigningKey(this.secret).parseClaimsJws(token).getBody();
        } catch (Exception var4) {
            claims = null;
        }

        return claims;
    }

    public String generateToken(UserDetails userDetails) {
        JwtUser jwtUser = (JwtUser)userDetails;
        Map<String, Object> claims = new HashMap(2);
        claims.put("sub", userDetails.getUsername());
        claims.put("iat", new Date());
        claims.put("username", jwtUser.getUsername());
        claims.put("role", jwtUser.getAuthorities());
        return this.generateToken((Map)claims);
    }

    public String getUsernameFromToken(String token) {
        String username;
        try {
            Claims claims = this.getClaimsFromToken(token);
            username = claims.getSubject();
        } catch (Exception var4) {
            username = null;
        }

        return username;
    }

    public Boolean isTokenExpired(String token) {
        try {
            Claims claims = this.getClaimsFromToken(token);
            Date expiration = claims.getExpiration();
            return expiration.before(new Date());
        } catch (Exception var4) {
            return true;
        }
    }

    public String refreshToken(String token) {
        String refreshedToken;
        try {
            Claims claims = this.getClaimsFromToken(token);
            claims.put("iat", new Date());
            refreshedToken = this.generateToken((Map)claims);
        } catch (Exception var4) {
            refreshedToken = null;
        }

        return refreshedToken;
    }

    public Boolean validateToken(String token, UserDetails userDetails) {
        JwtUser user = (JwtUser)userDetails;
        String username = this.getUsernameFromToken(token);
        return username.equals(user.getUsername()) && !this.isTokenExpired(token);
    }

    public JwtTokenUtil() {
    }

    public String getSecret() {
        return this.secret;
    }

    public void setSecret(final String secret) {
        this.secret = secret;
    }

    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof JwtTokenUtil)) {
            return false;
        } else {
            JwtTokenUtil other = (JwtTokenUtil)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                Object this$secret = this.getSecret();
                Object other$secret = other.getSecret();
                if (this$secret == null) {
                    if (other$secret != null) {
                        return false;
                    }
                } else if (!this$secret.equals(other$secret)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(final Object other) {
        return other instanceof JwtTokenUtil;
    }

    public int hashCode() {
        boolean PRIME = true;
        int result = 1;
        Object $secret = this.getSecret();
        result = result * 59 + ($secret == null ? 43 : $secret.hashCode());
        return result;
    }

    public String toString() {
        return "JwtTokenUtil(secret=" + this.getSecret() + ")";
    }
}
